'use client';

import { useToast } from '@/hooks/use-toast';
import { result } from '@/lib/utils';
import { cancelOrder } from '@/service/actions/order';
import ConfirmButton from '../common/confirm-button';
import { Button } from '../ui/button';

export default function CancelOrder({ orderId }: { orderId: number }) {
  const { toast } = useToast();
  return (
    <ConfirmButton
      title="取消订单"
      description="确认取消订单吗？"
      button={<Button variant="outline">取消订单</Button>}
      onClick={async () => {
        const { error } = await cancelOrder({ orderId });
        error
          ? toast({ title: error.message, variant: 'destructive' })
          : toast({ title: '取消成功' });
        return error ? result(error, error.message) : result();
      }}
    />
  );
}
